package com.jiyinit.web.core.business.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.jiyinit.web.core.business.entity.TabCoreTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.web.core.business.dto.TaskMessageRequest;
import com.jiyinit.web.core.business.entity.TabCoreTaskMessage;
import com.jiyinit.web.core.business.service.TaskMessageServiceImpl;
import com.jiyinit.web.core.business.vo.TaskMessageVo;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.logic.ValidateResult;
import com.jiyinit.web.core.common.util.DateUtil;
import com.jiyinit.web.core.common.util.UserUtil;

/**
 * 用户管理
 *
 * @author wuyuefeng
 * @version 1.0
 * @date 2017/4/13 9:01
 * @since 1.0
 */
@Controller
@RequestMapping("/taskMessage")
public class TaskMessageController {
	/**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
    
    @Resource(name = "core.taskMessageServiceImpl")
    private TaskMessageServiceImpl taskMessageServiceImpl;
    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;
    
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月12日 上午8:49:48
         * @param 
         * @return
     */
    @RequestMapping("/message")
    public String toTaskMessage(HttpServletRequest request,TaskMessageRequest taskMessageRequest) {
    	List<TaskMessageVo> taskMessageVos = new ArrayList<TaskMessageVo>();
    	taskMessageVos = taskMessageServiceImpl.findAllByTaskId(taskMessageRequest.getTaskId());
    	
    	List<TaskMessageVo> taskMessageVosSubs = new ArrayList<TaskMessageVo>();
    	
    	JSONArray json = new JSONArray();
    	DateUtil std = new DateUtil();
    	Integer currentUid = UserUtil.getCurrentUser().getUserId();
    	Integer taskUid = taskMessageRequest.getTaskUid();
    	
    	
    	
    	for(TaskMessageVo taskMessageVo : taskMessageVos){
    		
    		taskMessageVosSubs = taskMessageServiceImpl.findTaskMessageByParentId(taskMessageVo.getTaskMessageId());
    		StringBuilder messageCon = new StringBuilder();
    		messageCon.append(taskMessageVo.getUserName()+":"+taskMessageVo.getMessageContent()+
            		"</br>("+std.getFormatDateToString(taskMessageVo.getCreateTime(),"yyyy-mm-dd hh:MM:ss")+")</br>"
    				
    				);
    		if(taskMessageVosSubs.size()>0){
    			messageCon.append("-----------------------------------------------</br>");
    			for(TaskMessageVo taskMessageVosSub : taskMessageVosSubs){
    				messageCon.append("&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'><b>"+taskMessageVosSub.getUserName()+"</b></font>:"+taskMessageVosSub.getMessageContent()+
    	            		"</br>&nbsp;&nbsp;&nbsp;&nbsp;("+std.getFormatDateToString(taskMessageVosSub.getCreateTime(),"yyyy-mm-dd hh:MM:ss")+")</br>"+
    	    				"-----------------------------------------------</br>"
    	    		);
        		}
    		}
    		
    		
            JSONObject jo = new JSONObject();
            Integer create_uid = taskMessageVo.getCreateUid();
            String MessageCla = taskMessageVo.getMessageCla();
            MessageCla = currentUid==create_uid?"rightBubble":"leftBubble";
            
            jo.put("task_message_id", taskMessageVo.getTaskMessageId());
            jo.put("message_content", messageCon);
            jo.put("create_time", std.getFormatDateToString(taskMessageVo.getCreateTime(),"yyyy-mm-dd hh:MM:ss"));
            jo.put("create_uid", create_uid);
            jo.put("task_id", taskMessageVo.getTaskId());
            jo.put("message_cla", MessageCla);
            
            jo.put("task_title", taskMessageVo.getTaskTitle());
            jo.put("user_name", taskMessageVo.getUserName());
            json.add(jo);
        }
    	request.setAttribute("taskMessageVos", json);
        return "/taskmessage/message";
    }
    @ResponseBody
    @RequestMapping("/addMessage")
    public ResponseEntity<ResponseDto> addMessage(HttpServletRequest request,TaskMessageRequest taskMessageRequest) {
  
    	ValidateResult<TaskMessageRequest> validateResult = paramCheckLogic.checkParamValidate(taskMessageRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        
        TabCoreTaskMessage tabCoreTaskMessage = new TabCoreTaskMessage();
        BeanUtils.copyProperties(taskMessageRequest, tabCoreTaskMessage);
       
        Integer currentUid = UserUtil.getCurrentUser().getUserId();
        Integer taskUid = taskMessageRequest.getTaskUid();
        if(taskUid!=currentUid){
        	tabCoreTaskMessage.setMessageCla("leftBubble");
        }else{
        	tabCoreTaskMessage.setMessageCla("rightBubble");
        }
        tabCoreTaskMessage.setCreateTime(new Date());
        tabCoreTaskMessage.setCreateUid(currentUid);

        TabCoreTask task = new TabCoreTask();
        task.setEditTime(new Date());
        task.setTaskId(taskMessageRequest.getTaskId());
        int num = 0;
       num = taskMessageServiceImpl.addTaskMessage(tabCoreTaskMessage,task);
        
    	if (num > 0) {
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail(""), HttpStatus.OK);
    }
}
